$(function(){
		var wcn = $('#txtAccessWCN').val();
		var wid = $('#txtAccessWID').val();
		var rid = $('#txtEmbedReportId').val();
		var emburl = $('#txtReportEmbed').val();
		var access_key=$('#txtAccessKey').val();
		var scp=$('#scp').val();
  	 	$('#txtReportEmbed').val(emburl+"?reportId="+rid);
  	 	$("#embed-and-interact-steps-wrapper,#reportContainer").height($(window).height());
		_Embed_BasicEmbed(scp);
		
		$('#saveReport').on('mouseover',function(){
			$(this).css('background','#c8c8c8');
		}).on('mouseout',function(){
			$(this).css('background','rgb(234,234,234)');
		})
		$('#saveAsReport').on('mouseover',function(){
			$(this).css('background','rgb(234,234,234)');
		}).on('mouseout',function(){
			$(this).css('background','rgb(234,234,234)');
		})
		setInterval(function(){
				var disname=$('#txtdisplayName').val();
				if(disname!='defaultName'){
					// Get a reference to the embedded report HTML element
					var reportContainer = $('#reportContainer')[0];
					// Get a reference to the embedded report.
					var report = powerbi.get(reportContainer);
					// Save report
					report.save();
					console.log("save auto!")
				}
			},300000)
		//5 m save
		$("#saveReport").bind("click",function(){
			var disname=$('#txtdisplayName').val();
			var fileName = $('#txtprocessorId').val();
			if(disname=='defaultName'){
				// Get a reference to the embedded report HTML element
				var reportContainer = $('#reportContainer')[0];
				// Get a reference to the embedded report.
				var report = powerbi.get(reportContainer);
				var saveAsParameters = {
				    name: fileName
				};
				var wcn = $('#txtAccessWCN').val();
				var wid = $('#txtAccessWID').val();
				var rid = $('#txtEmbedReportId').val();
				var emburl = $('#txtReportEmbed').val();
				var txtimportID=$('#txtAccessimportID').val();
				var access_key=$('#txtAccessKey').val();
				var txtprocessorId=$('#txtprocessorId').val();
				var txtpage=$('#txtpage').val();
				var txtmodel=$('#txtmodel').val();
				$.ajax({
	               type: "POST",
	               url: "/powerbi/get/report/name",
	               data: {collectionName:wcn,workspaceID:wid,accessKey:access_key,importID:txtimportID,displayName:fileName},
	               headers: {"Content-type": "application/x-www-form-urlencoded;charset=UTF-8"},
	               success: function (data) {
	            	// SaveAs report
            		report.saveAs(saveAsParameters);
	                  if(data=='success'){
	                	  $.ajax({
	                          type: "POST",
	                          url: "/reportmodel/update/report/model",
	                          data: {collectionName:wcn,workspaceID:wid,accessKey:access_key,importID:txtimportID,displayName:fileName,processorId:txtprocessorId,page:txtpage,model:txtmodel},
	                          headers: {"Content-type": "application/x-www-form-urlencoded;charset=UTF-8"},
	                          success: function (data) {
	                             if(data=='success'){
	                            	alert('保存成功!');
	                            	$('#txtdisplayName').val(fileName);
	                             }else{
	                            	 alert('网络繁忙，稍后重试!');
	                             }
	                          }
	                      });
	                  }else{
	                	  alert('报表名已存在，请重新输入');
	                  }
	               }
	           });
			}else{
				// Get a reference to the embedded report HTML element
				var reportContainer = $('#reportContainer')[0];
				// Get a reference to the embedded report.
				var report = powerbi.get(reportContainer);
				// Save report
				report.save();
			}
		});
		//点击另存出现弹出层
//		$('#saveAsReport').on('click',function(){
//			$('#fileName').val('');
//			$('.mask').show();
//		})
		//弹出层取消
//		$('.cancel').on('click',function(){
//			$('.mask').hide();
//		})
		//弹出层保存操作
//		$('.store').on('click',function(){
//			// 文件另存情况下用户自定义名称
//			var fileName = $('#fileName').val();
//			if (fileName == '') {
//				alert('文件名称不可为空');
//			}else{
//				$('.mask').hide();
//				
//				// Get a reference to the embedded report HTML element
//				var reportContainer = $('#reportContainer')[0];
//				// Get a reference to the embedded report.
//				var report = powerbi.get(reportContainer);
//				var saveAsParameters = {
//				    name: fileName
//				};
//				var wcn = $('#txtAccessWCN').val();
//				var wid = $('#txtAccessWID').val();
//				var rid = $('#txtEmbedReportId').val();
//				var emburl = $('#txtReportEmbed').val();
//				var txtimportID=$('#txtAccessimportID').val();
//				var access_key=$('#txtAccessKey').val();
//				var txtprocessorId=$('#txtprocessorId').val();
//				var txtpage=$('#txtpage').val();
//				var txtmodel=$('#txtmodel').val();
//				$.ajax({
//	               type: "POST",
//	               url: "/powerbi/get/report/name",
//	               data: {collectionName:wcn,workspaceID:wid,accessKey:access_key,importID:txtimportID,displayName:fileName},
//	               headers: {"Content-type": "application/x-www-form-urlencoded;charset=UTF-8"},
//	               success: function (data) {
//	            	// SaveAs report
//            		report.saveAs(saveAsParameters);
//	                  if(data=='success'){
//	                	  $.ajax({
//	                          type: "POST",
//	                          url: "/reportmodel/update/report/model",
//	                          data: {collectionName:wcn,workspaceID:wid,accessKey:access_key,importID:txtimportID,displayName:fileName,processorId:txtprocessorId,page:txtpage,model:txtmodel},
//	                          headers: {"Content-type": "application/x-www-form-urlencoded;charset=UTF-8"},
//	                          success: function (data) {
//	                             if(data=='success'){
//	                            	alert('保存成功!');
//	                             }else{
//	                            	 alert('网络繁忙，稍后重试!');
//	                             }
//	                          }
//	                      });
//	                  }else{
//	                	  alert('报表名已存在，请重新输入');
//	                  }
//	               }
//	           });
//			}
//		})
});	
